const express = require("express");
const bcrypt = require("bcrypt");
const router = express.Router();
const User = require("../../models/User");

router.get("/users", async (req, res) => {
  const model = await User.find();
  res.send(model);
});

router.delete("/users/:id", async (req, res) => {
  const model = await User.findByIdAndRemove(req.params.id);
  res.send(model);
});

router.post("/users", async (req, res) => {
  const model = await User.create(req.body);
  res.send(model);
});

// router.get("/user", async (req, res) => {
//   const user = await User.findOne({
//     username: req.body.username
//   });
//   if (!user) {
//     return res.status(422).send({
//       message: "用户名不存在"
//     });
//   }
//   const isPasswordValid = bcrypt.compareSync(req.body.password, user.password);
//   if (!isPasswordValid) {
//     return res.status(422).send({
//       message: "密码无效"
//     });
//   }
//   res.send(user);
// });

module.exports = router;
